package com.zbkj.service.service.mszs;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.zbkj.common.model.mszs.MszsHotel;
import com.zbkj.common.model.mszs.MszsPlatformAccount;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.mszs.MszsPlatformAccountRequest;
import com.zbkj.common.response.mszs.MszsPlatformAccountResponse;
import com.zbkj.service.dao.mszs.MszsPlatformAccountDao;
import com.zbkj.service.service.mszs.MszsHotelService;
import com.zbkj.service.service.mszs.MszsPlatformAccountSevice;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 民宿助手-平台账号-servers
 * JIANTAO
 */
@Service
public class MszsPlatformAccountSeviceImpl extends ServiceImpl<MszsPlatformAccountDao, MszsPlatformAccount> implements MszsPlatformAccountSevice {

    @Autowired
    private MszsPlatformAccountDao accountDao;

    @Autowired
    private MszsHotelService hotelService;

    @Override
    public boolean insert(MszsPlatformAccountRequest request) {

        MszsPlatformAccount account = new MszsPlatformAccount();
        BeanUtils.copyProperties(request,account);

        return save(account);
    }

    @Override
    public boolean update(MszsPlatformAccountRequest request) {
        MszsPlatformAccount account = new MszsPlatformAccount();
        BeanUtils.copyProperties(request,account);

        return updateById(account);
    }

    @Override
    public boolean delete(Integer id) {
        return this.removeById(id);
    }

    @Override
    public MszsPlatformAccountResponse load(Integer id) {
        MszsPlatformAccount account = this.getById(id);
        MszsPlatformAccountResponse response = new MszsPlatformAccountResponse();
        BeanUtils.copyProperties(account,response);
        MszsHotel hotel = hotelService.getById(account.getJdId());
        response.setJdName(hotel.getJdName());
        return response;
    }

    @Override
    public List<MszsPlatformAccountResponse> pageList(String account,String jdId, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        LambdaQueryWrapper<MszsPlatformAccount> rapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(account)){
            rapper.like(MszsPlatformAccount::getAccount,account);
        }
        if (StringUtils.isNotEmpty(jdId)){
            rapper.like(MszsPlatformAccount::getJdId,jdId);
        }
        rapper.orderByDesc(MszsPlatformAccount::getCreateTime);
        List<MszsPlatformAccount> records = accountDao.selectList(rapper);

        List<MszsPlatformAccountResponse> responseList = records.stream().map(e -> {
            MszsPlatformAccountResponse response = new MszsPlatformAccountResponse();
            BeanUtils.copyProperties(e, response);
            MszsHotel hotel = hotelService.getById(e.getJdId());
            response.setJdName(hotel.getJdName());
            return response;
        }).collect(Collectors.toList());
        return responseList;
    }
}
